package com.wc.算法提高课.C第三章_图论.单源最短路的建图方式.昂贵的聘礼;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/10/10 15:10
 * @description https://www.acwing.com/problem/content/905/
 */
public class Main {
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 110, M = N * N, INF = 0x3f3f3f3f;
    static int[] h = new int[N], e = new int[M], ne = new int[M], w = new int[M];
    static int[] dist = new int[N], level = new int[N];
    static boolean[] st = new boolean[N];
    static PriorityQueue<int[]> heap = new PriorityQueue<>(Comparator.comparingInt(o -> o[0]));
    static int n, m, idx = 1;

    public static void main(String[] args) {
        m = sc.nextInt();
        n = sc.nextInt();
        for (int i = 1; i <= n; i++) {
            int p = sc.nextInt();
            level[i] = sc.nextInt();
            int cnt = sc.nextInt();
            add(0, i, p);
            while (cnt-- > 0) {
                int id = sc.nextInt(), cost = sc.nextInt();
                add(id, i, cost);
            }
        }
        int res = INF;
        for (int i = level[1] - m; i <= level[1]; i++) res = Math.min(res, dijkstra(i, i + m));
        out.println(res);
        out.flush();
    }

    static int dijkstra(int down, int up) {
        Arrays.fill(st, 0, n + 1, false);
        Arrays.fill(dist, 0, n + 1, INF);
        dist[0] = 0;
        heap.add(new int[]{dist[0], 0});
        while (!heap.isEmpty()) {
            int t = heap.poll()[1];
            if (st[t]) continue;
            st[t] = true;
            for (int i = h[t]; i > 0; i = ne[i]) {
                int j = e[i];
                if (down <= level[j] && level[j] <= up && dist[j] > dist[t] + w[i]) {
                    dist[j] = dist[t] + w[i];
                    heap.add(new int[]{dist[j], j});
                }
            }
        }
        return dist[1];
    }

    static void add(int a, int b, int c) {
        e[idx] = b;
        w[idx] = c;
        ne[idx] = h[a];
        h[a] = idx++;
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}
